from gm.api import *
import os

from MSCI_tools import 计算RSRS as RSRS


def init(context):
    schedule(schedule_func=algo, date_rule='1d', time_rule='09:31:00')

    context.index = 'SHSE.000300'


def algo(context):
    now = context.now
    last_day = get_previous_trading_date('SHSE', now)

    rsrs_weight = RSRS.get_rsrs_weight_classic(context.index, last_day)

    if rsrs_weight > 0.7:
        order_target_percent(symbol=context.index, percent=1, order_type=OrderType_Market,
                             position_side=PositionSide_Long)
    elif rsrs_weight < 0.7:
        order_target_percent(symbol=context.index, percent=0, order_type=OrderType_Market,
                             position_side=PositionSide_Long)


def on_backtest_finished(context, indicator):
    print(indicator)


if __name__ == '__main__':
    run(
        strategy_id='a5299b24-8b44-11e9-a4d4-b499baf0193a',
        filename=(os.path.basename(__file__)),
        # filename=('程序6_2策略.py'),
        mode=MODE_BACKTEST,
        token='90be3f863b23ab3c1ef68d1f9b8dc06e4bebb30d',
        backtest_start_time='2016-01-01 09:00:00',
        backtest_end_time='2018-12-31 15:00:00',
        backtest_initial_cash=10000000,
        backtest_adjust=ADJUST_PREV,
    )
    print(os.path.basename(__file__))
